package com.baidu.bas.core.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;



/**
 * @author liuzhenchuan@foxmail.com
 * @date 2021/3/6
 */
public class UrlUtil {
    public static boolean hasParams(String url){
        try {
            // 微信认证的页面带/# getQuery会截取掉
            url = url.replace("/#","");
            URI uri = new URI( url );
            return uri.getQuery()!=null;
        } catch (Exception e) {
            return false;
        }
    }

    private static final String SLASH = "/";

    public static String encode(String url){
        try {
            return URLEncoder.encode(url,"utf-8");
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }

    public static String concatRight(String path,String name){
        if(path.startsWith(SLASH)){
            path = path.substring(1);
        }
        if(!path.endsWith(SLASH)){
            path = path + SLASH;
        }
        return path + name;
    }

    public static String concat(String base, String path) {
        if (base.endsWith(SLASH)) {
            if (path.startsWith(SLASH)) {
                return base + path.substring(1);
            } else {
                return base + path;
            }
        } else {
            if (path.startsWith(SLASH)) {
                return base + path;
            } else {
                return base + SLASH + path;
            }
        }
    }

}
